package com.tz.farmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartPark.msframe.result.ResultEx;
import com.tz.farmer.mapper.FarmerProcessMapper;
import com.tz.farmer.model.FarmerProcess;
import com.tz.farmer.service.IFarmerProcessService;
import com.tz.farmer.so.DefaultSo;
import org.springframework.stereotype.Service;

@Service
public class FarmerProcessServiceImpl extends ServiceImpl<FarmerProcessMapper, FarmerProcess> implements IFarmerProcessService {
    @Override
    public IPage<FarmerProcess> selectPage(DefaultSo body, String market) {
        if (body.getPageNum() == null) body.setPageNum(1);
        if (body.getPageSize() == null) body.setPageSize(5);

        Page<FarmerProcess> page = new Page<>(body.getPageNum(), body.getPageSize());

        LambdaQueryWrapper<FarmerProcess> wrapper = new LambdaQueryWrapper<FarmerProcess>()
            .eq(FarmerProcess::getMarket, market);

        return page(page, wrapper);
    }

    @Override
    public ResultEx setProcess(FarmerProcess process, String market) {
        if (process != null) process.setMarket(market);
        // 验证是否更新成功
        if (saveOrUpdate(process)) {
            return ResultEx.builder().makeSuccessResult();
        }

        return ResultEx.builder().makeFailedResult();
    }
}
